<?php $this->pageHeader('Mine vakter'); ?>

<?php echo $this->flashMessage()->renderMessages(); ?>

<div class="header_wrapper">
    <h1>Mine vakter</h1>
    <?php echo $this->filterForm; ?>
</div>

<table class="core_shifts">
  <thead>
    <tr>
        <th>Navn</th>
        <th>Oppmøte</th>
        <th>Rolle</th>
        <th>&nbsp;</th>
    </tr>
  </thead>
  <tbody>
<?php
  foreach($this->shifts as $shift):
    $shiftName = '<a href="' . $this->url(array('module' => 'core', 'controller' => 'shift', 'action' => 'view', 'id' => $shift->id), null, true) . '">' . $this->escape($shift->name) . '</a>';
    $appearanceTime = new RedCross_Date();
    $appearanceTime->import($shift->appearance_time);
    $appearanceTime = $appearanceTime->getDateTimeStr();

    $toDate = new RedCross_Date($shift->to_date);
    $nowDate = new RedCross_Date();

    $rolesOnShift = $shift->getMemberRoles($this->authInfo()->getMemberId());

    $isLeaderOnShift = false;
    if ($rolesOnShift):
        $multipleRoles = count($rolesOnShift) > 1;
        foreach ($rolesOnShift as $role):
            if ((bool) $role->leader):
                $isLeaderOnShift = true;
                break;
            endif;
        endforeach;
    endif;

    $isShiftReportApproved = (!is_null($shift->shift_report_id) && (bool)$shift->ShiftReport->approved);
    $isShiftReportInTheFuture = $nowDate->isEarlier($toDate);
    $showSubmitReportText = !$isShiftReportInTheFuture && $isLeaderOnShift && !$isShiftReportApproved;
    $submitShiftReportText =
        $showSubmitReportText ?
        ' (<a href="' . $this->url(array('module' => 'core', 'controller' => 'shift', 'action' => 'report', 'id' => $shift->id), null, true) . '">Lag vaktrapport</a>)' :
        '';

    if (!$multipleRoles):
        $roleName = $rolesOnShift[0]->Role->name;
?>
            <tr>
                <td><?php echo $shiftName . $submitShiftReportText; ?></td>
                <td><?php echo $appearanceTime; ?></td>
                <td><?php echo $roleName; ?></td>
                <td>
                <?php if ($nowDate->isEarlier($appearanceTime)):?>
                    <?php echo is_null($rolesOnShift[0]->notification_confirmed_time) ?
                            '<a href="' . $this->url(array('module' => 'core', 'controller' => 'shift', 'action' => 'confirm', 'id' => $rolesOnShift[0]->id, 'key' => RedCross_Global::shift_role_hash($rolesOnShift[0])), null, true) . '">Bekreft</a>' :
                            'Bekreftet';
                    ?>
                    <?php
                    // TODO: This is ugly... Have to loop through the shift 
                    // exchanges in order to see if there are any unconfirmed 
                    // shift exchanges for the current shift...
                    // TODO: Only show shift exchange link for future shifts?
                    $printShiftExchangeLink = true;
                    foreach ($rolesOnShift[0]->Member->ShiftExchanges as $shiftExchange):
                        if ($shiftExchange->shift_role_id == $rolesOnShift[0]->id && is_null($shiftExchange->request_handled_time)):
                            $printShiftExchangeLink = false; 
                            $shiftExchangeId = $shiftExchange->id;
                            break;
                        endif;
                    endforeach; ?>
                    <?php if ($printShiftExchangeLink): ?>
                        | <a href="<?php echo $this->url(array('module' => 'core', 'controller' => 'shift-exchange', 'action' => 'select-member', 'id' => $rolesOnShift[0]->id), null, true); ?>">Bytt vakt</a>
                    <?php else: ?>
                        | <a href="<?php echo $this->url(array('module' => 'core', 'controller' => 'shift-exchange', 'action' => 'cancel', 'id' => $shiftExchangeId), null, true); ?>">Avbryt vaktbytte</a>
                    <?php endif; ?>
                <?php endif; ?>
                </td>
            </tr>
<?php else: ?>
            <tr>
                <td><?php echo $shiftName . $submitShiftReportText; ?></td>
                <td><?php echo $appearanceTime; ?></td>
                <td>
                    <ul>
                        <?php foreach ($rolesOnShift as $role): ?>
                            <li><?php echo $role->Role->name; ?></li>
                        <?php endforeach; ?>
                    </ul>
                </td>
                <td>

                <?php if ($nowDate->isEarlier($appearanceTime)):?>
                    <ul>
                        <?php foreach ($rolesOnShift as $role): ?>
                            <li>
                                <?php echo
                                    is_null($role->notification_confirmed_time) ?
                                    '<a href="' . $this->url(array('module' => 'core', 'controller' => 'shift', 'action' => 'confirm', 'id' => $role->id, 'key' => RedCross_Global::shift_role_hash($role)), null, true) . '">Bekreft</a>' :
                                    'Bekreftet';
                                ?> 
                                <?php
                                // TODO: This is ugly... Have to loop throught the shift 
                                // exchanges in order to see if there are any unconfirmed 
                                // shift exchanges for the current shift...
                                // TODO: Only show shift exchange link for future shifts?
                                $printShiftExchangeLink = true;
                                foreach ($role->Member->ShiftExchanges as $shiftExchange):
                                    if ($shiftExchange->shift_role_id == $role->id && is_null($shiftExchange->request_handled_time)):
                                        $printShiftExchangeLink = false; 
                                        $shiftExchangeId = $shiftExchange->id;
                                        break;
                                    endif;
                                endforeach; ?>
                                <?php if ($printShiftExchangeLink): ?>
                                    | <a href="<?php echo $this->url(array('module' => 'core', 'controller' => 'shift-exchange', 'action' => 'select-member', 'id' => $role->id), null, true); ?>">Bytt vakt</a>
                                <?php else: ?>
                                    | <a href="<?php echo $this->url(array('module' => 'core', 'controller' => 'shift-exchange', 'action' => 'cancel', 'id' => $shiftExchangeId), null, true); ?>">Avbryt vaktbytte</a>
                                <?php endif; ?>
                            </li>
                        <?php endforeach; ?>
                    </ul>
                <?php endif; ?>
                </td>
            </tr>
<?php endif; ?>
<?php endforeach; ?>
    </tbody>
</table>
